<!--
 * @Author: caiwq caiwq@putron.com
 * @Date: 2022-07-29 11:03:25
 * @LastEditors: caiwq caiwq@putron.com
 * @LastEditTime: 2022-08-19 13:34:36
 * @FilePath: \LMX\src\components\setting.vue
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
-->
<template>
  <div class="setting_content">
    <div class="setting_box">
      <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
        <el-form-item label="用户名" prop="username">
          <el-input v-model.number="ruleForm.username"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="pass">
          <el-input :row="4" type="password" v-model="ruleForm.pass" autocomplete="off" show-password placeholder="请重新输入密码"></el-input>
        </el-form-item>
        <el-form-item label="确认密码" prop="checkPass">
          <el-input type="password" v-model="ruleForm.checkPass" autocomplete="off" show-password placeholder="请再次确认密码"></el-input>
        </el-form-item>
        <div class="btnBox">
          <div class="subBtn"  @click="submitForm('ruleForm')">保存</div>
        </div>
      </el-form>
    </div>
  </div>
</template>

<script>
import { LMX_sendDataApi } from "../api/qiehuan";
export default {
  data() {
    var checkusername = (rule, value, callback) => {
      if (!value) {
        return callback(new Error('用户名不能为空'));
      }
      callback();
    };
    var validatePass = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入密码'));
      } else {
        if (this.ruleForm.checkPass !== '') {
          this.$refs.ruleForm.validateField('checkPass');
        }
        callback();
      }
    };
    var validatePass2 = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请再次输入密码'));
      } else if (value !== this.ruleForm.pass) {
        callback(new Error('两次输入密码不一致!'));
      } else {
        callback();
      }
    };
    return {
      ruleForm: {
        username: '',
        pass: '',
        checkPass: '',
      },
      rules: {
        pass: [
          { validator: validatePass, trigger: 'blur' }
        ],
        checkPass: [
          { validator: validatePass2, trigger: 'blur' }
        ],
        username: [
          { validator: checkusername, trigger: 'blur' }
        ]
      }
    };
  },
  mounted() {
    let token = localStorage.getItem("lmx_token");
    this.ruleForm.username = token.split("分割符")[0];
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          // alert('submit!');
          LMX_sendDataApi({
            editUPs: {
              userName: this.ruleForm.username + "",
              password: this.ruleForm.pass
            },
          }).then(() => {
            this.$message.success("修改成功");
            localStorage.removeItem("lmx_token");
            this.$router.push({ path: "/" });
          });

        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
  },
  watch: {
    "ruleForm.username"(item1) {
      this.ruleForm.username = item1.replaceAll(" ", "")
    },
    "ruleForm.pass"(item1) {
      this.ruleForm.pass = item1.replaceAll(" ", "")
    },
    "ruleForm.checkPass"(item1) {
      this.ruleForm.checkPass = item1.replaceAll(" ", "")
    },
  }
}
</script>

<style scoped lang="scss">
.subBtn{
  width: 100px;
  height: 40px;
  line-height: 40px;
  text-align: center;
  color:#3c590b;
  background: #a2ce3a;
}
.el-input {
  width: 80%;
}

.setting_content {
  height: 400px;
  margin-top: 150px;
  display: flex;
  justify-content: center;
  align-content: center;

  .setting_box {
    box-sizing: border-box;
    padding-top: 80px;
    width: 30%;
    height: 100%;
    border-radius: 20px;

    h1 {
      text-align: center;
      margin-bottom: 50px;
    }

    .btn_item {
      display: flex;
      justify-content: center;
      align-content: center;
    }

    .btnBox {
      display: flex;
      justify-content: center;
      align-content: center;
    }

  }
}
</style>